package com.scau.action.business;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.scau.action.BaseAction;
import com.scau.exception.BusinessException;
import com.scau.model.business.Material;
import com.scau.model.business.Purchase;
import com.scau.model.business.PurchaseDetail;
import com.scau.model.business.Supplier;
import com.scau.model.business.SupplierMaterial;
import com.scau.model.view.PurchaseDetailView;
import com.scau.service.impl.business.MaterialService;
import com.scau.service.impl.business.PurchaseDetailService;
import com.scau.service.impl.business.PurchaseService;
import com.scau.service.impl.business.SupplierService;
import com.scau.service.impl.view.PurchaseDetailViewService;
import com.scau.util.PageController;

@Component
@Scope("prototype")
public class PurchaseDetailAction extends BaseAction implements Serializable {
	private static final long serialVersionUID = 8299975587235537983L;
	private final static Log logger = LogFactory.getLog(ModelRuleAction.class);
	private PageController pager = null;
	private PurchaseDetail purchaseDetail = null;
	private PurchaseDetailService purchaseDetailService = null;
	private PurchaseDetailView purchaseDetailView = null;
	private PurchaseDetailViewService purchaseDetailViewService = null;
	private List<Material> materialList = null;
	private MaterialService materialService = null;
	private List<Supplier> supplierList = null;
	private SupplierService supplierService = null;
	private PurchaseService purchaseService;
	private SupplierMaterialService supplierMaterialService;
	private Purchase purchase;
	private SupplierMaterial supplierMaterial;

	/**
	 * 列表显示
	 * 
	 * @return
	 */
	public String list() {
		String purchase_id = request.getParameter("purchase_id");		
//		purchaseDetailView.setPurchase_id(purchase_id);
		request.setAttribute("purchase_id", purchase_id);
		purchaseDetailView=new PurchaseDetailView();
		purchase = new Purchase();		
		int totalRows = purchaseDetailViewService
				.getRecordCount(purchaseDetailView);
		String URL = getListURL();
		this.pager.setURL(URL);
		this.pager.setTotalRowsAmount(totalRows);
		if(null!=purchase_id&&""!=purchase_id&&"0"!=purchase_id){
			Long id=Long.parseLong(purchase_id);
			purchaseDetailView.setPurchase_id(id);			
			purchase = purchaseService.get(purchase,id);
		}
		List<PurchaseDetailView> resourceList = purchaseDetailViewService.list(
				purchaseDetailView, this.pager.getPageStartRow(),
				pager.getPageSize(), null, null);
		pager.setData(resourceList);	
		request.setAttribute("purchase", purchase);
		return "list";
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String get() {
		String purchase_id=request.getParameter("purchase_id");
		System.out.println(purchase_id);
		request.setAttribute("purchase_id", purchase_id);
		purchaseDetailView = purchaseDetailViewService.get(purchaseDetailView);
		materialList = materialService.listAll(new Material());
		supplierList = supplierService.listAll(new Supplier());
		request.setAttribute("materialList", materialList);
		request.setAttribute("supplierList", supplierList);
		return "edit";
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		String purchase_id = request.getParameter("purchase_id");
		request.setAttribute("purchase_id", purchase_id);
		String materialDatas = request.getParameter("MaterialDatas");
		try {					
			String[] materialData = materialDatas.split(",");//得到material.id
			supplierMaterial=new SupplierMaterial();
			supplierMaterial.setMaterial_id(Long.parseLong(materialData[0]));//设置supplierMaterial.material_id
			
			String supplier_id=request.getParameter("supplier_id");
			supplierMaterial.setSupplier_id(Long.parseLong(supplier_id));
			Long supplier_material_id=supplierMaterialService.save(supplierMaterial);
			purchaseDetail.setSupplier_material_id(supplier_material_id);
			purchaseDetail.setPurchase_id(Long.parseLong(purchase_id));//设置采购单的id	
			purchaseDetail.setStatus(0l);
			purchaseDetailService.save(purchaseDetail);
			return list();
		} catch (BusinessException e) {
			// 保存原来表单已输入的内容
			request.setAttribute("purchaseDetailView", purchaseDetailView);
			request.setAttribute("message", e.getMessage());
			return "edit";
		}
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String del() {
		String purchase_id=request.getParameter("purchase_id");
		request.setAttribute("purchase_id", purchase_id);
		String[] ids = request.getParameterValues("id");
		purchaseDetail = new PurchaseDetail();
		for (String Id : ids) {
			if (null != Id && !("".equals(Id))) {
				purchaseDetailService
						.delete(purchaseDetail, Long.parseLong(Id));
			}
		}
		return list();
	}

	/**
	 * 确认采购
	 * 
	 * @return
	 */
	public String comfirm() {
		purchaseDetail = purchaseDetailService.get(purchaseDetail);
		Material material = new Material();
//		material.setId(purchaseDetail.getMaterial_id());
		material = materialService.get(material);
		material.setRepertory(material.getRepertory()
				+ purchaseDetail.getAmount());// 材料入库（修改库存）
		purchaseDetail.setStatus(1L);// 改变采购状态
		materialService.update(material);// 完成入库
		purchaseDetailService.update(purchaseDetail);
		return list();
	}

	/**
	 * 打印
	 * @return
	 */
	public String print() {
		Long purchase_id = Long.parseLong(request.getParameter("purchase_id"));
		purchaseDetailView.setPurchase_id(purchase_id);
		int totalRows = purchaseDetailViewService
				.getRecordCount(purchaseDetailView);
		String URL = getListURL();
		this.pager.setURL(URL);
		this.pager.setTotalRowsAmount(totalRows);
		List<PurchaseDetailView> resourceList = purchaseDetailViewService.list(
				purchaseDetailView, this.pager.getPageStartRow(),
				pager.getPageSize(), null, null);
		pager.setData(resourceList);
		Purchase purchase = new Purchase();
		purchase.setId(purchase_id);
		purchase = purchaseService.get(purchase);
		request.setAttribute("purchase", purchase);
		return "print";
	}

	public PageController getPager() {
		return pager;
	}

	@Resource
	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public PurchaseDetail getPurchaseDetail() {
		return purchaseDetail;
	}

	public void setPurchaseDetail(PurchaseDetail purchaseDetail) {
		this.purchaseDetail = purchaseDetail;
	}

	public PurchaseDetailService getPurchaseDetailService() {
		return purchaseDetailService;
	}

	@Resource
	public void setPurchaseDetailService(
			PurchaseDetailService purchaseDetailService) {
		this.purchaseDetailService = purchaseDetailService;
	}

	public PurchaseDetailView getPurchaseDetailView() {
		return purchaseDetailView;
	}

	public void setPurchaseDetailView(PurchaseDetailView purchaseDetailView) {
		this.purchaseDetailView = purchaseDetailView;
	}

	public PurchaseDetailViewService getPurchaseDetailViewService() {
		return purchaseDetailViewService;
	}

	@Resource
	public void setPurchaseDetailViewService(
			PurchaseDetailViewService purchaseDetailViewService) {
		this.purchaseDetailViewService = purchaseDetailViewService;
	}

	public PurchaseService getPurchaseService() {
		return purchaseService;
	}

	@Resource
	public void setPurchaseService(PurchaseService purchaseService) {
		this.purchaseService = purchaseService;
	}

	public List<Material> getMaterialList() {
		return materialList;
	}

	public void setMaterialList(List<Material> materialList) {
		this.materialList = materialList;
	}

	public MaterialService getMaterialService() {
		return materialService;
	}
	
	@Resource
	public void setMaterialService(MaterialService materialService) {
		this.materialService = materialService;
	}

	public List<Supplier> getSupplierList() {
		return supplierList;
	}

	public void setSupplierList(List<Supplier> supplierList) {
		this.supplierList = supplierList;
	}

	public SupplierService getSupplierService() {
		return supplierService;
	}

	@Resource
	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}

	public SupplierMaterialService getSupplierMaterialService() {
		return supplierMaterialService;
	}

	@Resource
	public void setSupplierMaterialService(SupplierMaterialService supplierMaterialService) {
		this.supplierMaterialService = supplierMaterialService;
	}

}
